import json
import random
import time

from flask import Blueprint, render_template, request, session, jsonify, make_response, url_for
import logging

from app.config.config import config
from app.settings import env
from common import response_message
from common.utils import compress_image, model_to_json
from model.article import Article
from model.favorite import Favorite
from model.feedback import Feedback
from model.user import User

article = Blueprint("article", __name__)
label_types = config[env].label_types
article_types = config[env].article_types
article_tags = config[env].article_tags


@article.before_request
def article_before_request():
    url = request.path
    is_login = session.get("is_login")
    if url.startswith("/article") and "new" in url and is_login != 'true':
        response = make_response("登录重定向", 302)
        response.headers["Location"] = url_for("index.home")
        return response


@article.route("/detail")
def article_detail():
    article_id = request.args.get("article_id")
    article = Article()
    # 获取文章的所有信息
    article_content = article.get_article_detail(article_id)
    article_tag_string = article_content.article_tag
    article_tag_list = article_tag_string.split(",")

    # 获取文章作者信息
    user = User()
    user_info = user.find_by_userid(article_content.user_id)
    feedback_data_list = Feedback().get_feedback_user_list(article_id)

    is_favorite = 1

    if session.get("is_login") == "true":
        user_id = session.get("user_id")
        is_favorite = Favorite().user_if_favorite(user_id, article_id)
    # 查询评论的数量
    feedback_count = Feedback().get_article_feedback_count(article_id)

    # 相关文章的功能
    about_article = article.find_about_article(article_content.label_name)
    return render_template("article-info.html",
                           article_content=article_content,
                           user_info=user_info,
                           is_favorite=is_favorite,
                           article_tag_list=article_tag_list,
                           about_article=about_article,
                           feedback_data_list=feedback_data_list,
                           feedback_count=feedback_count)


@article.route("/article/new")
def article_new():
    user_id = session.get("user_id")
    # 我的草稿相关实现
    all_drafted = Article().get_all_article_drafted(user_id)

    return render_template("new-article.html",
                           label_types=label_types,
                           article_types=article_types,
                           article_tags=article_tags,
                           all_drafted=all_drafted,
                           drafted_count=len(all_drafted))


# 获取某一篇草稿的详情
@article.route("/article/drafted", methods=["post"])
def get_drafted_detail():
    request_data = json.loads(request.data)
    result = Article().get_one_artcile_drafted(request_data.get('id'))
    #    把结果转成json，然后给前端返回
    article_drafted = model_to_json(result)
    return response_message.ArticleMessage.success(article_drafted)


def get_article_request_param(request_data):
    user = User().find_by_userid(session.get("user_id"))
    title = request_data.get("title")
    article_content = request_data.get("article_content")
    return user, title, article_content


# 草稿或文章存储
@article.route("/article/save", methods=["post"])
def article_save():
    request_data = json.loads(request.data)
    # 我们根据article_id来判断是不是第一次保存，如果没有这个id就存储为草稿，如果有那么就文章发布
    # 其实文章发布就是文章更新
    article_id = request_data.get("article_id")
    # 取出是否是草稿
    drafted = request_data.get("drafted")
    # 必须让前端传一个article_id，那么这个值如果是-1我们就认为是草稿
    if article_id == -1 and drafted == 0:
        user, title, article_content = get_article_request_param(request_data)
        if title == "":
            return response_message.ArticleMessage.other("请输入文章头信息")
        # 存储草稿的时候一定要返回一个article_id回来
        article_id = Article().insert_article(user.user_id, title, article_content, drafted)
        return response_message.ArticleMessage.save_success(article_id, "草稿存储成功")
    elif article_id > -1:
        user, title, article_content = get_article_request_param(request_data)
        if title == "":
            return response_message.ArticleMessage.other("请输入文章头信息")
        # 图片信息就不在这里获取了，当用户点击上传头像的时候，这个头像信息就应该已经更新到数据库里了
        # 所以图片上传这个动作应该发生在文章发布的前边
        label_name = request_data.get("label_name")
        article_tag = request_data.get("article_tag")
        article_type = request_data.get("article_type")

        article_id = Article().update_article(
            article_id=article_id,
            title=title,
            article_content=article_content,
            drafted=drafted,
            label_name=label_name,
            article_tag=article_tag,
            article_type=article_type
        )
        return response_message.ArticleMessage.save_success(article_id, "发布文章成功")


# 上传文章头部图片
@article.route("/article/upload/article_header_image", methods=["post"])
def upload_article_header_image():
    # 获取前端图片文件
    f = request.files.get("header-image-file")
    filename = f.filename

    # 文件的后缀名
    suffix = filename.split(".")[-1]
    newname = time.strftime("%Y%m%d_%H%M%S." + suffix)
    newname = "article-header-" + newname
    f.save("resource/upload/" + newname)
    # 大图片压缩
    source = dest = "resource/upload/" + newname
    compress_image(source, dest, 1200)

    # 更新数据库
    article_id = request.form.get("article_id")
    Article().update_article_header_image(article_id, newname)
    # 构造响应数据
    result = {}
    result["state"] = "SUCCESS"
    result['url'] = "/upload/" + newname
    result["title"] = filename
    result["original"] = filename
    return jsonify(result)


@article.route("/article/random/header/image", methods=["post"])
def random_article_header_image():
    name = random.randint(1, 539)
    newname = str(name) + ".jpg"
    # 更新数据库
    article_id = request.form.get("article_id")
    Article().update_article_header_image(article_id, newname)
    # 构造响应数据
    result = {}
    result["state"] = "SUCCESS"
    result['url'] = "/images/headers/" + newname
    result["title"] = newname
    result["original"] = newname
    return jsonify(result)
